package common;
import java.util.ArrayList;

/**
 * Round Trip Time.
 * 
 * @author Brad & Adam
 */
public class RTT {
  private ArrayList<Long> rtts = new ArrayList<Long>();
  
  /**
   * Default Constructor.
   */
  public RTT() {
    this(new long[0]);
  }
  
  /**
   * Constructor with single time.
   * 
   * @param time
   */
  public RTT(long time) {
    this(new long[] { time });
  }
  
  /**
   * Constructor to use when averaging.
   * 
   * @param times
   */
  public RTT(long[] times) {
    for (int i = 0; i < times.length; i++) {
      rtts.add(times[i]);
    }
  }
  
  /**
   * Add.
   * 
   * @param time
   */
  public void add(long time) {
    rtts.add(time);
  }
  
  /**
   * get Average.
   * 
   * @return
   */
  public long getAverage() {
    long sum = 0;
    for (int i = 0; i < rtts.size(); i++) {
      sum += rtts.get(i);
    }
    long avg = (long) Math.ceil((double) sum / (double) rtts.size());
    return avg;
  }
  
  /**
   * For testing/debugging.
   * 
   * @param args
   */
  public static void main(String[] args) {
    long[] times = { 3000, 2000, 5000 };
    RTT rtts = new RTT(times);
    System.out.println(rtts.getAverage());
    rtts = new RTT();
    System.out.println(rtts.getAverage());
    rtts = new RTT(3000);
    System.out.println(rtts.getAverage());
  }
}
